//
//  MessageSessionDao.m
//  SouthChina
//
//  Created by creator_lq on 14/12/4.
//  Copyright (c) 2014年 科创. All rights reserved.
//

#import "MessageSessionDao.h"
#import "DataDictDao.h"
#import "Class+Plugins.h"
#import "DataDict.h"

@implementation DataDictDao



/*!
 *  跟新字典数据
 *
 *  @param appList <#appList description#>
 *
 *  @return <#return value description#>
 */
+(BOOL)updataDBwithSQLiteStatement:(NSArray*)appList{
    
    
    FMDatabase *_dataBase = [DateBaseHelper openDateBase];
    
    [self checkTableCreatedInDb:_dataBase];
    
    [_dataBase beginTransaction];
    
    BOOL isRollBack = NO;
    @try {
        NSString * delsql = [NSString stringWithFormat:@"DELETE from datadict "];
        [_dataBase executeUpdate:delsql];
        
        for (int i = 0; i<[appList  count]; i++) {
            

            
            NSMutableDictionary *map = [[NSMutableDictionary alloc] initWithDictionary:[appList objectAtIndex:i]];
            NSString *dictdataId= [map objectForKey:DICTDATAID];
            NSString *dictdataName= [map objectForKey:DICTDATANAME];
            NSString *dictdataOrder= [map objectForKey:DICTDATAORDER];
            NSString *dictdataValue= [map objectForKey:DICTDATAVALUE];
            NSString *dicttypeId= [map objectForKey:DICTTYPEID];
            NSString *dictdataDesc = [map objectForKey:DICTDATADESC];
            //跟新时间
            NSDate *updatime= [[map objectForKey:UPDATE_TIME] dateFromString];
            
            [map removeObjectForKey:DICTDATAID];
            [map removeObjectForKey:DICTDATANAME];
            [map removeObjectForKey:DICTDATAORDER];
            [map removeObjectForKey:DICTDATAVALUE];
            [map removeObjectForKey:DICTTYPEID];
            [map removeObjectForKey:DICTDATADESC];
            
            NSString *otherinf = [map JSONString];
            
            NSString *sql = @"INSERT INTO datadict (dictdataId,dictdataName,dictdataOrder,dictdataValue,dicttypeId,dictdataDesc,OTHER_INFO,UPDATE_TIME) VALUES (?,?,?,?,?,?,?,?)";
            
            BOOL a =[_dataBase executeUpdate:sql,dictdataId,dictdataName,dictdataOrder,dictdataValue,dicttypeId,dictdataDesc,otherinf,updatime];
            
            if(!a){
                NSLog(@"失败");
            }else{
                //                NSLog(@"成功=%@",appname);
            }
            
        }
    }
    @catch (NSException *exception) {
        
        NSLog(@"插入失败--------: %@",exception);
        isRollBack = YES;
        [_dataBase rollback];
    }
    @finally {
        if (!isRollBack) {
            [_dataBase commit];
        }
    }
    
    [_dataBase close];
    
    return YES;
    
}


/*!
 *  跟新字典数据
 *
 *  @param appList <#appList description#>
 *
 *  @return <#return value description#>
 */
+(BOOL)updataDataDictWithType:(NSArray*)appList dicttypeId:(NSString*) dicttypeId
{
    
    FMDatabase *_dataBase = [DateBaseHelper openDateBase];
    
    [self checkTableCreatedInDb:_dataBase];
    
    [_dataBase beginTransaction];
    
    BOOL isRollBack = NO;
    @try {
        NSString * delsql = [NSString stringWithFormat:@"DELETE from datadict where dicttypeId ='%@'",dicttypeId];
        [_dataBase executeUpdate:delsql];
        
        for (int i = 0; i<[appList  count]; i++) {
            
            
            NSMutableDictionary *map = [[NSMutableDictionary alloc] initWithDictionary:[appList objectAtIndex:i]];
            NSString *dictdataId= [map objectForKey:DICTDATAID];
            NSString *dictdataName= [map objectForKey:DICTDATANAME];
            NSString *dictdataOrder= [map objectForKey:DICTDATAORDER];
            NSString *dictdataValue= [map objectForKey:DICTDATAVALUE];
            NSString *dicttypeId= [map objectForKey:DICTTYPEID];
            NSString *dictdataDesc = [map objectForKey:DICTDATADESC];
            //校验了列表中的类型是否是跟新的类型
            if(![dicttypeId isEqualToString:dicttypeId])
                continue;
            
            //跟新时间
            NSDate *updatime= [[map objectForKey:UPDATE_TIME] dateFromString];
            
            [map removeObjectForKey:DICTDATAID];
            [map removeObjectForKey:DICTDATANAME];
            [map removeObjectForKey:DICTDATAORDER];
            [map removeObjectForKey:DICTDATAVALUE];
            [map removeObjectForKey:DICTTYPEID];
            [map removeObjectForKey:DICTDATADESC];
            
            
            NSString *otherinf = [map JSONString];
            
            NSString *sql = @"INSERT INTO datadict (dictdataId,dictdataName,dictdataOrder,dictdataValue,dicttypeId,dictdataDesc,OTHER_INFO,UPDATE_TIME) VALUES (?,?,?,?,?,?,?,?)";
            
            BOOL a =[_dataBase executeUpdate:sql,dictdataId,dictdataName,dictdataOrder,dictdataValue,dicttypeId,dictdataDesc,otherinf,updatime];
            
            if(!a){
                NSLog(@"失败");
            }else{
                //                NSLog(@"成功=%@",appname);
            }
            
        }
    }
    @catch (NSException *exception) {
        
        NSLog(@"插入失败--------: %@",exception);
        isRollBack = YES;
        [_dataBase rollback];
    }
    @finally {
        if (!isRollBack) {
            [_dataBase commit];
        }
    }
    
    [_dataBase close];
    
    return YES;
    
}


/*!
 *  根据查询条件查询字典数据
 *
 *  @param condition <#condition description#>
 *
 *  @return <#return value description#>
 */
+(NSMutableArray*)queryDataDict:(NSString*)condition{
    
    NSMutableArray *dataDictList = [[NSMutableArray alloc] init];
    FMDatabase *_dataBase = [DateBaseHelper openDateBase];
    [self checkTableCreatedInDb:_dataBase];
    
    NSString * sql = [NSString stringWithFormat:@"SELECT * from datadict %@",condition];
    
    NSLog(@"执行数据库语句：%@",sql);
    
    FMResultSet *rs=[_dataBase executeQuery:sql];

    while ([rs next]) {
        DataDict * dataDict = [[DataDict alloc] init];
        dataDict.dictdataId     =[rs stringForColumn:DICTDATAID];
        dataDict.dictdataName   =[rs stringForColumn:DICTDATANAME];
        dataDict.dictdataOrder  =[rs stringForColumn:DICTDATAORDER];
        dataDict.dictdataValue  =[rs stringForColumn:DICTDATAVALUE];
        dataDict.dicttypeId     =[rs stringForColumn:DICTTYPEID];
        dataDict.otherInfo      =[rs stringForColumn:OTHER_INFO];
        dataDict.upate_time     =[rs dateForColumn:UPDATE_TIME];
        dataDict.dictdataDesc   =[rs stringForColumn:DICTDATADESC];
        [dataDictList addObject:dataDict];
    };
    [rs close];
    [_dataBase close];
    return dataDictList;
}


+(BOOL)checkTableCreatedInDb:(FMDatabase *)db
{
    NSString *createStr=@"CREATE  TABLE  IF NOT EXISTS 'datadict' ('dictdataId' text PRIMARY KEY  NOT NULL  UNIQUE ,'dictdataName' text,'dictdataOrder' text, 'dictdataValue' text,'dicttypeId' text,'dictdataDesc' text,'OTHER_INFO' text,'UPDATE_TIME' date)";
    BOOL worked = [db executeUpdate:createStr];
    FMDBQuickCheck(worked);
    return worked;
}






@end
